package com.web.kdl.service.createTable;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Author： shulibin
 * @Date： 2025/8/21 18:02
 * @Describe：
 */

public class DataTable implements FlywheelTable{
    protected final String siteName;
    protected final String matrixName;
    protected final String flywheelName;
    protected final TableDataType type;

    public DataTable(String siteName, String matrixName, String flywheelName, TableDataType type) {
        this.siteName = siteName;
        this.matrixName = matrixName;
        this.flywheelName = flywheelName;
        this.type = type;
    }


    public String  createTable() {
    String sql = String.format("CREATE TABLE IF NOT EXISTS `%s` (" +
            "`id` bigint NOT NULL AUTO_INCREMENT," +
            "`sum_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '站点'," +
            "`sub_site` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '飞轮'," +
            "`time` datetime DEFAULT NULL COMMENT '时间'," +
            "`month` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '月份 例如:2025-06',"+
            "`data_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '飞轮内部设备'," +
            "`data_value` double(32,2) DEFAULT NULL COMMENT '数据'," +
            "`unit` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '单位'," +
            "`device_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '数据类型'," +
            "PRIMARY KEY (`id`) USING BTREE," +
            "INDEX `idx_device_month_time` (`device_name`(50),`month`,`time` DESC),"+
            "INDEX `idx_device_time_id`(`device_name`(50) ASC, `time` DESC, `id` DESC,`unit`) USING BTREE," +
            "KEY `idx_cursor_page` (`sum_site`,`sub_site`,`time`,`device_name`)," +
            "KEY `idx_time` (`time`)" +
             ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;", getTableName());
        return sql;
    }
    public String getTableName() {
        return String.format("wheel_data_%s_%s_%s_%s",type.name().toLowerCase(),siteName,matrixName,flywheelName);
    }

    public String deleteTable() {
        return String.format("DROP TABLE IF EXISTS `%s`", getTableName());
    }

    @Override
    public void insertData(Object data) {

    }

    @Override
    public TableType getType() {
        return null;
    }


}
